/*
 * @Author: your name
 * @Date: 2021-02-01 15:23:40
 * @LastEditTime: 2021-02-04 17:49:25
 * @LastEditors: your name
 * @Description: In User Settings Edit
 * @FilePath: \wwwd:\凯哥购物项目\pages\goods_list\index.js
 */
import { request } from "../../request/index";

Page({


    /**
     * 页面的初始数据
     */
    data: {
        tabs: [{
                id: 0,
                value: "综合",
                isActive: true
            },
            {
                id: 1,
                value: "降序",
                isActive: false
            },
            {
                id: 2,
                value: "升序",
                isActive: false
            },
        ],
        goodsList: []

    },


    QueryParams: {
        query: "",
        cid: "",
        pagenum: 1,
        pagesize: 10
    },
    totalPages: 1,

    onLoad: function(options) {
        this.QueryParams.cid = options.cid || "";
        this.QueryParams.query = options.query || "";
        this.getGoodList();
    },
    //请求数据
    async getGoodList() {
        const res = await request({ url: "/goodsList/"+this.QueryParams.cid+"/",  });
            console.log(res);
        this.setData({
                goodsList:res
            })
        wx.stopPullDownRefresh();
    },

   async handleTabsItemChange(e) {
        const { index } = e.detail;
        let { tabs } = this.data;
        var sortF;
        tabs.forEach((v, i) => i === index ? v.isActive = true : v.isActive = false);
        this.setData({
            tabs
        })
        if(index==2){
            sortF="1"
        }else if(index==1){
            sortF="0"
        }else{
        var res = await request({ url: "/goodsList/"+this.QueryParams.cid+"/",  });
        }
        if(sortF){
            var res = await request({ url: "/priceSort/5/sortF="+sortF });
        }
        this.setData({
            goodsList:res
        })
    wx.stopPullDownRefresh();
    },
    //上拉下一页
    onReachBottom() {
        if (this.QueryParams.pagenum >= this.totalPages) {
            wx: wx.showToast({
                title: '没有更多数据了',
            })
        }
        else {
            this.QueryParams.pagenum++;
            this.getGoodList();
        }
    },
    //下拉刷新
    onPullDownRefresh() {
        //重置数组
        this.setData({
            goodsList: []
        })
        this.QueryParams.pagenum = 1;
        this.getGoodList();
    }
})